<template>
  <div id="ApplicationSettlement">
    <p style="height: 50px;display: flex;align-items: center;border-bottom: 1px solid #D2D2D2;font-size: 20px;padding: 0 10px;">
      <span style="margin-right: 10px;" class="line"></span>
      申请入驻
    </p>
    <van-form show-error class="vanForm">
      <div class="inputWrap">
        <span style="color: #FF0000;">*</span>
        <van-field v-model="formData.houseapplicant" label="工匠姓名" placeholder="请输入真实姓名" />
      </div>
      <div class="inputWrap">
        <span style="color: #FF0000;">*</span>
        <van-field v-model="formData.certificatecode" label="身份证号码" placeholder="请输入身份证号" />
      </div>
      <div class="inputWrap">
        <van-field v-model="formData.zsbh" label="证书编号" placeholder="请输入证书编号" />
      </div>
      <div class="inputWrap">
        <span style="color: #FF0000;">*</span>
        <van-field v-model="formData.applyphone" label="联系电话" placeholder="请输入联系电话" :rules="[
              { required: true, message: '请填写您的联系电话！' },
              { pattern: /^1[3456789]\d{9}$/, message: '格式错误！'}
            ]" name="applyphone" />
      </div>
      <div class="inputWrap">
        <van-field v-model="formData.gznx" label="工作年限" placeholder="请输入工作年限" />
      </div>
      <div class="inputWrap">
        <van-field v-model="formData.scgz" label="擅长工种" placeholder="请输入擅长工种" />
      </div>
      <div class="inputWrap">
        <van-field readonly clickable name="picker" :value="ValueCity" label="所属市" placeholder="请选择所属市" @click="showPickerC = true" />
        <van-popup v-model="showPickerC" position="bottom" get-container="#popup">
          <van-picker show-toolbar :columns="CityColumns" value-key="CITYNAME" @confirm="onConfirmCity" @cancel="showPickerC = false" />
        </van-popup>
      </div>
      <div class="inputWrap">
        <van-field readonly clickable name="picker" :value="valueXian" label="所属县(区)" placeholder="请选择所属县(区)" @click="showPickerX = true" />
        <van-popup v-model="showPickerX" position="bottom" get-container="#popup">
          <van-picker show-toolbar :columns="XianColumns" value-key="CITYNAME" @confirm="onConfirmXian" @cancel="showPickerX = false" />
        </van-popup>
      </div>
      <div class="inputWrap">
        <span> 上传证书照片</span>
        <van-uploader v-model="fileList" style="margin-top:10px" accept="image/jpeg, image/png, image/jpg, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/pdf" :after-read="afterRead" :max-count="1" />
      </div>
    </van-form>
    <div style="height: 50px;background: #007AFF;line-height: 50px;text-align: center;color: #ffffff;font-weight: bold;" @click="BtnSaveFormData">提交申请</div>
  </div>
</template>
<script>
import { dataServerOpenDataView, FileServerUpload } from '@/utils/api'
export default {
  data() {
    return {
      showPickerC: false,
      showPickerX: false,
      formData: {
        // HOUSEAPPLICANT: '',
        houseapplicant: '',
        // CERTIFICATECODE: '',
        certificatecode: '',
        // APPLYPHONE: '',
        applyphone: "",
        zsbh: '',
        gznx: '',
        scgz: '',
        // S_CITYNAME: '',
        S: '',
        // X_CITYNAME: '',
        X: '',
        // TOWN_CITYNAME: '',
        town: '',
        // CUN_CITYNAME: '',
        CUN: '',
        // IF_TDZ_DICNAME: '',
        If_ljf: '',
        // BUILDTYPE_DICNAME: '',
        buildtype: '',
        // BUILDREASON_DICNAME: ''
        buildreason: ''
      },
      fileList: [],
    }
  },
  methods: {
    onConfirmCity(value) {
      this.cityCode = value.CITYCODE
      this.ValueCity = value.CITYNAME
      // this.formData.S_CITYNAME = value.CITYNAME;
      // this.formData.S_CITYNAME = value.CITYCODE;
      this.formData.S = value.CITYCODE;
      this.showPickerC = false;
      this.getXianOpenDataView(value.CITYCODE)
    },
    onConfirmXian(value) {
      this.valueXian = value.CITYNAME
      // this.formData.X_CITYNAME = value.CITYNAME;
      // this.formData.X_CITYNAME = value.CITYCODE;
      this.formData.X = value.CITYCODE;
      this.showPickerX = false;
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      this.getFileServerUpload(file.file)
    },
    getFileServerUpload: async function (file) {
      let formData = new FormData();
      formData.append('file', file)
      formData.append('toRowGuid', 'ae9ad536-24e6-4437-8e03-62fbf1f8b259')
      formData.append('groupGuid', '1f15519a-1f7a-4605-a099-68dbb47fc9db')
      formData.append('token', window.localStorage.getItem('token'))
      const res = await FileServerUpload(formData)
      if (res.data.Success == true) {
        this.$toast.success('上传成功!');
        // this.$notify({ type: 'success', message: '上传成功!' });
      }
    },
    getCityOpenDataView: async function () {
      this.CityColumns = []
      let dataGuid = 'e9c98787-4966-4365-80b0-c1cc678c6f85'
      let searchObject = []
      let token = window.localStorage.getItem('token')
      const res = await dataServerOpenDataView(dataGuid, searchObject, token)
      this.CityColumns = res.data.Data
    },
    getXianOpenDataView: async function (data) {
      this.XianColumns = []
      let dataGuid = '77fdf2b0-0eb5-4243-a0a4-6185299ccd34'
      let searchObject = encodeURIComponent(JSON.stringify([{ "field": "pid", "value": data, "operator": "=" }]))
      let token = window.localStorage.getItem('token')
      const res = await dataServerOpenDataView(dataGuid, searchObject, token)
      this.XianColumns = res.data.Data
    },
    BtnSaveFormData() {
      if (this.formData.houseapplicant && this.formData.certificatecode && this.formData.applyphone) {
        this.$router.push({
          path: "/ApplicationProcess",
        });
      } else {
        this.$toast.fail('请输入必选项！');
        // this.$notify({
        //   type: "danger",
        //   message: "请输入必选项！",
        // });
      }

    }
  },
  mounted() {
    this.getCityOpenDataView()
  },
}
</script>
<style lang="scss">
#ApplicationSettlement {
  height: 100vh;
  overflow: auto;
  .van-cell:not(:last-child)::after {
    left: 0;
    border-bottom: 1px solid rgb(210, 210, 210);
  }
  .van-cell {
    padding: 10px 0;
    font-size: 16px;
  }
  .van-field__control {
    text-align: right;
  }
  .line {
    width: 6px;
    height: 26px;
    background: #007aff;
    display: inline-block;
  }
  .inputWrap {
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #d2d2d2;
    padding: 0 10px;
    min-height: 40px;
    line-height: 40px;
    justify-content: space-between;
  }
  .vanForm {
    width: 100%;
    height: calc(100vh - 100px);
    overflow: auto;
    display: block;
  }
}
</style>
